<?php
/**
 * Created by PhpStorm.
 * Clerk: Administrator
 * Date: 2018/8/24
 * Time: 10:28
 */

namespace app\api\model;

use think\Db;
use think\Model;

class Order extends Model {
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    /**
     * 插入数据
     * @param $token
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public static function setOrder($order_data,$goods_data,$iden){
        // 启动事务
        Db::startTrans();
        try {
            $order_res = Db::name('order')->insertGetId($order_data);
            $goods_detail = Db::name('goods')->field('price,offer_price,name')->where('id',$goods_data['goods_id'])->find();
            $goods_data['goods_name'] = $goods_detail['name'];
            $goods_data['order_id'] = $order_res;
            if($iden == 'client'){
                $goods_data['goods_price'] = $goods_detail['price'];
            }else if($iden == 'clerk'){
                $goods_data['goods_price'] = $goods_detail['offer_price'];
            }else{
                return false;
            }
            $goods_res = Db::name('order_goods')->insertGetId($goods_data);
            // 提交事务'
            Db::commit();
            if($goods_res){
                return $order_res;
            }else{
                return false;
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }

    }

    //获取订单
    public static function getOrder($order_id,$order = 0){
        if($order){
            $data = Db::name('order')
                ->alias('a')
                ->field('b.name,b.phone,b.address,a.order_money,c.goods_number,c.goods_price')
                ->join('client b','a.client_id=b.id','left')
                ->join('order_goods c','a.id = c.order_id','left')
                ->join('goods d','c.goods_id = d.id')
                ->where('a.id',$order_id)
                ->select();

        }else{
            $data = Db::name('order')
                ->alias('a')
                ->field('b.name,b.phone,b.address,a.order_money,c.goods_number,c.goods_price')
                ->join('clerk b','a.clerk_id=b.id','left')
                ->join('order_goods c','a.id = c.order_id','left')
                ->join('goods d','c.goods_id = d.id')
                ->where('a.id',$order_id)
                ->select();
        }
        if($data){
            return $data;
        }else{
            return false;
        }
    }

    public static function getOrderList($order_status,$clerk_id){
        $data_clerk = Db::name('order')
            ->alias('a')
            ->field('b.name,b.phone,b.address,a.order_money,c.goods_number,c.goods_price,d.name as goods_name,d.img,d.goods_content')
            ->join('clerk b','a.clerk_id=b.id','left')
            ->join('order_goods c','a.id = c.order_id','left')
            ->join('goods d','c.goods_id = d.id')
            ->where('a.order_status',$order_status)
            ->where('a.client_id','=','0')
            ->where('a.clerk_id','=',$clerk_id)
            ->group('a.id')
            ->select();

        $data_client = Db::name('order')
            ->alias('a')
            ->field('b.name,b.phone,b.address,a.order_money,c.goods_number,c.goods_price,d.name as goods_name,d.img,d.goods_content')
            ->join('client b','a.client_id=b.id','left')
            ->join('order_goods c','a.id = c.order_id','left')
            ->join('goods d','c.goods_id = d.id')
            ->where('a.order_status',$order_status)
            ->where('a.client_id','<>','0')
            ->where('a.clerk_id','=',$clerk_id)
            ->group('a.id')
            ->select();

        $data = array_merge($data_clerk,$data_client);
        return $data;
    }

    public static function getSumMoney($clerk_id,$time){
        //dump($time);exit;
        $data = Db::name('order')
            ->where(['clerk_id'=>$clerk_id,'order_status'=>'4'])
            ->where('create_time','>',$time[0])
            ->where('create_time','<=',$time[1])
            ->sum('order_money');
        return $data;
    }

    public static function getOrderDetail($clerk_id,$time){
        $data_clerk = Db::name('order')
            ->alias('a')
            ->field('b.name,b.phone,b.address,a.order_money,c.goods_number,c.goods_price,d.name as goods_name,d.img,d.goods_content')
            ->join('clerk b','a.clerk_id=b.id','left')
            ->join('order_goods c','a.id = c.order_id','left')
            ->join('goods d','c.goods_id = d.id')
            ->where('a.create_time','>',$time[0])
            ->where('a.create_time','<=',$time[1])
            ->where('a.client_id','=','0')
            ->where('a.clerk_id','=',$clerk_id)
            ->where('a.order_status','4')
            ->group('a.id')
            ->select();

        $data_client = Db::name('order')
            ->alias('a')
            ->field('b.name,b.phone,b.address,a.order_money,c.goods_number,c.goods_price,d.name as goods_name,d.img,d.goods_content')
            ->join('client b','a.client_id=b.id','left')
            ->join('order_goods c','a.id = c.order_id','left')
            ->join('goods d','c.goods_id = d.id')
            ->where('a.create_time','>',$time[0])
            ->where('a.create_time','<=',$time[1])
            ->where('a.client_id','<>','0')
            ->where('a.clerk_id','=',$clerk_id)
            ->where('a.order_status','4')
            ->group('a.id')
            ->select();

        $data = array_merge($data_clerk,$data_client);
        return $data;
    }



}